package com.windward.vii.web.controller;

import com.windward.vii.common.JsonResult;
import com.windward.vii.service.UserAttentionService;
import com.windward.vii.service.UserTokenService;
import com.windward.vii.utils.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@Controller
@RequestMapping(value = "/")
public class UserAttentionController {

    private static Logger logger = LoggerFactory.getLogger(UserAttentionController.class);

    @Resource
    UserAttentionService userAttentionService;
    @Resource
    UserTokenService userTokenService;

    @RequestMapping(value = "v1/api1/ugc/attention/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult attention(@RequestParam Long targetUid,
                                @RequestHeader(value="token") String token) {
        Long uid = userTokenService.getUidByToken(token);
        return userAttentionService.attention(uid, targetUid);
    }

    @RequestMapping(value = "v1/api1/ugc/attention/cancel", method = RequestMethod.POST)
    @ResponseBody
    public JsonResult cancelAttention(@RequestParam Long targetUid,
                                @RequestHeader(value="token") String token) {
        Long uid = userTokenService.getUidByToken(token);
        return userAttentionService.cancelAttention(uid, targetUid);
    }

    @RequestMapping(value = "v1/api1/ugc/attention/check", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult checkIsAttention(@RequestParam Long targetUid,
                                             @RequestHeader(value="token") String token) {
        Long uid = userTokenService.getUidByToken(token);
        if(userAttentionService.checkIsAttention(uid, targetUid)) {
            JsonResult jsonResult = JsonResult.success("已关注");
            jsonResult.setBody(true);
            return jsonResult;
        } else {
            JsonResult jsonResult = JsonResult.success("未关注");
            jsonResult.setBody(false);
            return jsonResult;
        }
    }

    @RequestMapping(value = "v1/api0/ugc/attention/count", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getAttentionCountByUid(@RequestParam Long uid) {
        return userAttentionService.getAttentionCountByUid(uid);
    }

    @RequestMapping(value = "v1/api0/ugc/fans/count", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getFansCountByUid(@RequestParam Long uid) {
        return userAttentionService.getFansCountByUid(uid);
    }

    @RequestMapping(value = "v1/api0/ugc/attention/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getAttentionListByUid(@RequestParam Long uid, @RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                            @RequestHeader(value="User-Agent") String userAgent) {
        logger.info("pageNum={}", pageNum);
        logger.info("pageSize={}", pageSize);
        pageNum = pageNum > 0 ? pageNum : 1;
        return userAttentionService.getAttentionListByUid(uid, pageNum, pageSize);
    }

    @RequestMapping(value = "v1/api0/ugc/fans/list", method = RequestMethod.GET)
    @ResponseBody
    public JsonResult getFansListByUid(@RequestParam Long uid, @RequestParam Integer pageNum, @RequestParam Integer pageSize,
                                       @RequestHeader(value="User-Agent") String userAgent) {
        logger.info("pageNum={}", pageNum);
        logger.info("pageSize={}", pageSize);
        pageNum = pageNum > 0 ? pageNum : 1;
        return userAttentionService.getFansListByUid(uid, pageNum, pageSize);
    }

}
